package com.example.myapplication.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.myapplication.activity.LoadingActivity;
import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.VideoRecommendAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.RecommendVideo;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class VideoRecommendFragment  extends Fragment {
    private View view;
    private RecyclerView recycler_view_video;
    private VideoRecommendAdapter adapter;
    private List<RecommendVideo.DatasBean> datasBeanList = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_video_recommend, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        recycler_view_video = view.findViewById(R.id.recycler_view_video);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext());
        recycler_view_video.setLayoutManager(manager);
        adapter = new VideoRecommendAdapter(getContext());
        recycler_view_video.setAdapter(adapter);
        loadData(0);
        SwipeRefreshLayout swipeRefreshLayout = view.findViewById(R.id.refresh_layout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            int count = 0;
            @Override
            public void onRefresh() {
                Toast.makeText(getContext(), "加载数据中..", Toast.LENGTH_SHORT).show();
                count++;
                loadData(count);
                swipeRefreshLayout.setRefreshing(false);
            }
        });

    }

    public void loadData(int offset) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<RecommendVideo> call = service.getRecommendVideo(LoadingActivity.cookie,offset);
        call.enqueue(new Callback<RecommendVideo>() {
            @Override
            public void onResponse(Call<RecommendVideo> call, Response<RecommendVideo> response) {
                RecommendVideo baseResponse = response.body();
                datasBeanList = baseResponse.getDatas();
                adapter.setData(datasBeanList);
            }

            @Override
            public void onFailure(Call<RecommendVideo> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
